package com.wzm.moviepic;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.wzm.bean.AdInfo;
import com.wzm.bean.DownInfo;
import com.wzm.bean.PicInfo;
import com.wzm.cache.ConfigCache;
import com.wzm.db.DBHelper;
import com.wzm.manager.AppConfig;
import com.wzm.manager.AppManager;
import com.wzm.manager.Tag;
import com.wzm.utils.Logger;
import com.wzm.utils.NetworkUtils;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.os.Environment;


public class AppApplication extends Application {

	public static String mSdcardDataDir ;
	public static int mNetWorkState = NetworkUtils.NETWORN_NONE;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		instance = this;
		initFileDir();
		//dList.clear();
		
		//获取队列信息
		getDownListInfo();		
	
	}
	
	public void initFileDir()
	{
		//初始化地址
		if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            File file = new File(Environment.getExternalStorageDirectory().getPath() +  AppConfig.URLDIR);
            if(!file.exists()) {
                if (file.mkdirs()) {
                    mSdcardDataDir = file.getAbsolutePath();
                }
            } else {
                mSdcardDataDir = file.getAbsolutePath();
            }
        }
		//获取初始化网络
		mNetWorkState = NetworkUtils.getNetworkState(this);
		if(mNetWorkState!=NetworkUtils.NETWORN_NONE)
		{
			AppManager.getInstance(this).postsaveInfo();
		}else{
			//提取数据解析
			String cachejson = ConfigCache.getUrlTagCache(AppConfig.TAG_URL);
			if(null!=cachejson)
			{
				executeCacheJson(cachejson);
			}
		}
		
		
		//获取版本号
		try {
			Tag.VerNum = String.valueOf(getVersionCode());
			
		} catch (NameNotFoundException e) {
		}
		Logger.info("versionNo:"+Tag.VerNum);
	}
	//解析缓存数据
	public void executeCacheJson(String cacheJson)
	{
		Logger.info(cacheJson);
		try {
			JSONObject result = new JSONObject(cacheJson);
			JSONArray dlist = result.getJSONArray("ding");
			for(int i=0;i<dlist.length();i++)
			{
				String movieid = dlist.getString(i);
				AppManager.getInstance(instance).getDingList().add(movieid);
			}
			Logger.info("ding:"+AppManager.getInstance(instance).getDingList().size());
			
			
			JSONArray clist = result.getJSONArray("cai");
			for(int i=0;i<clist.length();i++)
			{
				String movieid = clist.getString(i);
				AppManager.getInstance(instance).getCaiList().add(movieid);
			}
			Logger.info("cai:"+AppManager.getInstance(instance).getCaiList().size());
			
			
			JSONArray plist = result.getJSONArray("played");
			for(int i=0;i<clist.length();i++)
			{
				String movieid = clist.getString(i);
				AppManager.getInstance(instance).getPlayedList().add(movieid);
			}
			Logger.info("played:"+AppManager.getInstance(instance).getPlayedList().size());
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Logger.error("jsonexception:"+e.getMessage());
		}
	}
	
	
	// 影片末尾广告
	ArrayList<AdInfo> adbList = new ArrayList<AdInfo>();
	
	public ArrayList<AdInfo> getAdbList() {
		return adbList;
	}

	public void setAdbList(ArrayList<AdInfo> adbList) {
		this.adbList = adbList;
	}
	

	private Map<String, ArrayList<PicInfo>> fileMap = new HashMap<String,ArrayList<PicInfo>>();
	public Map<String, ArrayList<PicInfo>> getFileMap() {
		return fileMap;
	}
	
	public void addMovie(String movieid,ArrayList<PicInfo> list)
	{
		fileMap.put(movieid, list);
	}
	


	private ArrayList<DownInfo> dList = new ArrayList<DownInfo>();
	public ArrayList<DownInfo> getDownList()
	{
		return dList;
	}
	public void setDList(ArrayList<DownInfo> dlist)
	{
		dList = dlist;
	}
	
	
	
	private static AppApplication instance;  
	  
    public static AppApplication getInstance() {  
        return instance;  
    }  
    
    
    
    private Map<String,DownInfo> fMap = new LinkedHashMap<String,DownInfo>();  
    public Map<String, DownInfo> getfMap() {
		return fMap;
	}

	

	private Map<String,DownInfo> dMap = new LinkedHashMap<String,DownInfo>();
    public Map<String,DownInfo> getDMap()
    {
    	//dMap.
    	return dMap;
    }
    
    //当前下载中的电影id
    int CurDownMovie = 0;
    //目前队列中的下载index
    int CurDownid = 0;
  
    public int getCurDownid() {
		return CurDownid;
	}

	public void setCurDownid() {
		CurDownid = CurDownid+1;
	}
	
	public void setCurDownid(int downid)
	{
		CurDownid = downid;
	}
	
	
	public int getCurDownMovie() {
		return CurDownMovie;
	}

	public void setCurDownMovie(int curDownMovie) {
		CurDownMovie = curDownMovie;
	}

	//获取下载队列
	public void getDownListInfo()
	{
		//获取数据库下载队列
		Cursor cursor = DBHelper.getInstance(this).querySql("select * from downinfo order by id asc", null);
		if(null!=cursor&&cursor.getCount()>0)
		{
			Logger.error("下载队列:"+cursor.getCount());
			//dList.clear();
			if(cursor.moveToFirst())
			{
				do{
					DownInfo di = new DownInfo();
					di.setMoviename(cursor.getString(4));
					di.setId(cursor.getInt(0));
					//单前下载到的页面
					di.setCurpage(cursor.getInt(3));
					Logger.info("电影ID："+String.valueOf(cursor.getInt(1)));
					di.setMovieid(String.valueOf(cursor.getInt(1)));
					di.setSize(cursor.getInt(2));
					//dList.add(di);
					
					if(cursor.getInt(5)==0)
					{//未下载完成
						Logger.info("未完成");
						dMap.put(di.getMovieid(), di);
					}else{
						Logger.info("完成");
						fMap.put(di.getMovieid(), di);
					}
					
					
					di = null;
					
				}while(cursor.moveToNext());
			}		
						
			
		}else{
			Logger.error("下载队列为空");
		}
		
		if(null!=cursor)
		{
			cursor.close();
		}						
	}
    
    
	
    private int getVersionCode() throws NameNotFoundException 
    {  
        PackageManager packageManager = getPackageManager();  
        PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),0);  
        int version = packInfo.versionCode;  
        Tag.VerName = packInfo.versionName;
        return version;  
    }  
    

}
